﻿Public Class searchUser
    Inherits System.Web.UI.Page
    Dim DBManager As New DBManager()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
        lblError.Visible = False
        If (DropDownList1.SelectedValue.Equals("Email Address")) Then
            GridView2.DataSourceID = SqlDataSource1.ID
            GridView2.DataBind()
        Else
            GridView2.DataSourceID = SqlDataSource2.ID
            GridView2.DataBind()
        End If
        If (GridView2.Rows.Count <= 0) Then
            lblError.Visible = True
            lblError.Text = "Data is incorrect"
        End If
    End Sub

    Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
        Dim stringVal As String

        If e.CommandName = "Promote" Then
            If (DropDownList1.SelectedValue.Equals("Email Address")) Then
                SqlDataSource1.UpdateParameters("is_admin").DefaultValue = "true"
                GridView2.SelectRow(e.CommandArgument)

                Dim row As GridViewRow = GridView2.SelectedRow

                stringVal = row.Cells(0).Text
                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal
                SqlDataSource1.Update()
                GridView2.SelectRow(-1)

            ElseIf (DropDownList1.SelectedValue.Equals("Display Name")) Then
                SqlDataSource2.UpdateParameters("is_admin").DefaultValue = "true"
                GridView2.SelectRow(e.CommandArgument)

                Dim row As GridViewRow = GridView2.SelectedRow

                stringVal = row.Cells(0).Text
                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal
                SqlDataSource2.Update()
                GridView2.SelectRow(-1)
            End If
        ElseIf e.CommandName = "Demote" Then
            If (DropDownList1.SelectedValue.Equals("Email Address")) Then
                SqlDataSource1.UpdateParameters("is_admin").DefaultValue = "false"
                GridView2.SelectRow(e.CommandArgument)

                Dim row As GridViewRow = GridView2.SelectedRow

                stringVal = row.Cells(0).Text
                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal
                SqlDataSource1.Update()
                GridView2.SelectRow(-1)

            ElseIf (DropDownList1.SelectedValue.Equals("Display Name")) Then
                SqlDataSource2.UpdateParameters("is_admin").DefaultValue = "false"
                GridView2.SelectRow(e.CommandArgument)

                Dim row As GridViewRow = GridView2.SelectedRow

                stringVal = row.Cells(0).Text
                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal
                SqlDataSource2.Update()
                GridView2.SelectRow(-1)
            End If
        End If
    End Sub

    Protected Sub GridView2_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowCreated
        Dim row As GridViewRow = e.Row
        Dim columns As List(Of TableCell) = New List(Of TableCell)
        Dim cell As TableCell

        For Each column As DataControlField In GridView2.Columns
            cell = row.Cells(0)
            row.Cells.Remove(cell)
            columns.Add(cell)
        Next
        row.Cells.AddRange(columns.ToArray())
    End Sub
End Class